<?php

namespace App\Console\Commands;

use App\Events\PostSaved;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;

class Ibox extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ibox';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    protected $urlConfig = [
        'Ibox-赛博terrier' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=100&albumId=100513863&order=1&onSale=1',
//        'iBox−赛博布偶' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=50&albumId=100513774&onSale=1&order=1',
//        'iBox−赛博边牧#38823' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=100&albumId=100513726&order=1&onSale=1',
//        'iBox-CyberBull' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=100&albumId=100513871&order=1&onSale=1',
//        'Im Sorry' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=100&albumId=100000393&order=1&onSale=1',
//        '功夫猫-2号' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=100&albumId=100513859&order=1&onSale=1',
//        'iBox-CyberMouse' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=50&albumId=100513784&onSale=1&order=1',
//        'iBox-玄牛遂意' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=50&albumId=100000254&onSale=1&order=1',

    ];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        if (env('APP_ENV') == 'dev') {
            $config = $this->urlConfig;
        } else {
            $config = ['Ibox-赛博terrier' => 'https://api-h5.ibox.art/nft-mall-web/v1.2/nft/product/getProductListByAlbumId?page=1&pageSize=100&albumId=100513863&order=1&onSale=1'];
        }

        while (1) {
            foreach ($config as $title => $url) {
                $res = $this->curl_get($url);
                $res = json_decode($res, 1);
                if ($res['code'] == 1) {
                    //获取最新的第一个价格
                    $p = $res['data']['list'][0]['priceCny'];

                    //组装数据
                    $lists = collect($res['data']['list']);
                    $list = collect($lists);
                    $saleing = $list->where('gStatus', 6)->take(5);//寄售
                    $lock = $list->where('gStatus', 16)->take(8);//锁定
                    $priceSaleStr = $saleing->pluck('priceCny')->implode('----');
                    $pricelockStr = $lock->pluck('priceCny')->implode('----');

                    $webhook = "https://oapi.dingtalk.com/robot/send?access_token=2bc88c77c09b7fc92e6ade1a100b1251a76e333f33690bf731d631c325859924";
                    if ($title == 'Ibox-赛博terrier' && ($p >= 1700)) {
//                        $data = array('msgtype' => 'text', 'at' => ['atMobiles' => [18238647889]],'text' => array('content' => '通知:' . date('Y-m-d H:i:s') . '，' . $title . '前5个寄售中的价格是' . $priceSaleStr . '，前五个锁定中的价格是' . $pricelockStr));
//                        $data = array('msgtype' => 'text', 'at' => ['atMobiles' => [18238647889]], 'text' => array('content' => '通知:' . date('Y-m-d H:i:s') . '，' . $title . '价格已经到' . $p));
                        $data = array('msgtype' => 'text', 'at' => ['atMobiles' => [18238647889, 15560174484]], 'text' => array('content' => '通知:' . date('Y-m-d H:i:s') . '，' . $title . '前5个寄售中的价格是' . $priceSaleStr . '，前五个锁定中的价格是' . $pricelockStr));
                    } else {
                        $data = array('msgtype' => 'text', 'text' => array('content' => '通知:' . date('Y-m-d H:i:s') . '，' . $title . '前5个寄售中的价格是' . $priceSaleStr . '，前五个锁定中的价格是' . $pricelockStr));
                    }


                    $data_string = json_encode($data);
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $webhook);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                    curl_exec($ch);
                    curl_close($ch);
                }
            }
            sleep(5);
        }


    }

    function curl_get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        if ($output === false) {
            echo "CURL Error:" . curl_error($ch);
        }
        curl_close($ch);

        return $output;
    }

    public function select($pwd)
    {


        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://tl21538988.lanzoum.com/filemoreajax.php',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => 'lx=2&fid=4922826&uid=1913492&pg=1&rep=0&t=1651119682&k=965c47e52a19313ca774561dadbfbfe8&up=1&vip=0&webfoldersign=&ls=1&pwd=' . $pwd,
            CURLOPT_HTTPHEADER => array(
                'Host:  tl21538988.lanzoum.com',
                'Connection:  keep-alive',
                'Content-Length:  127',
                'Accept:  application/json, text/javascript, */*',
                'X-Requested-With:  XMLHttpRequest',
                'User-Agent:  Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Mobile Safari/537.36',
                'Content-Type:  application/x-www-form-urlencoded',
                'Origin:  https://tl21538988.lanzoum.com',
                'Sec-Fetch-Site:  same-origin',
                'Sec-Fetch-Mode:  cors',
                'Sec-Fetch-Dest:  empty',
                'Referer:  https://tl21538988.lanzoum.com/b0270udxg',
                'Accept-Encoding:  gzip, deflate, br',
                'Accept-Language:  zh,zh-TW;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6',
                'Cookie:  codelen=1; UM_distinctid=1806b468cc0ea9-07cf05947fac28-6b3e555b-1e6000-1806b468cc1110a; pc_ad1=1; uz_distinctid=1806b4727e6593-0e6b985d4f63c9-6b3e555b-1e6000-1806b4727e7115c; STDATA82=czst_eid%3D404897775-3821-https%253A%252F%252Ftl21538988.lanzoum.com%252F%26ntime%3D3821; CNZZDATA568507=cnzz_eid%3D435872968-1651056969-%26ntime%3D1651110987; CNZZDATA1253610885=630056680-1651066403-%7C1651109610; CNZZDATA1253610888=1464381545-1651060841-%7C1651109444'
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return json_decode($response, 1);

    }

    function scanFile($path)
    {
        global $result;

        $files = scandir($path);

        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                if (is_dir($path . '/' . $file)) {
                    $this->scanFile($path . '/' . $file);
                } else {
                    $result[$path][] = basename($file);
                }
            }

        }

        //处理
        foreach ($result as $v)
            $tmp_name = str_replace('F1-SEO', 'F78874', $v);


        return $result;

    }

    function transTemplate2TextHtml(string $template, string ...$args): array
    {
        return [
            'text' => sprintf($template, ... array_map(function ($v) {
                return strip_tags($v);
            }, $args)),
            'html' => sprintf($template, ... $args)
        ];
    }

    /**
     * 根据key删除数组中指定元素
     * @param array $arr 数组
     * @param string/int  $key  键（key）
     * @return array
     */
    private function array_remove_by_key($arr, $key)
    {
        if (!array_key_exists($key, $arr)) {
            return $arr;
        }
        $keys = array_keys($arr);
        $index = array_search($key, $keys);
        if ($index !== FALSE) {
            array_splice($arr, $index, 1);
        }

        return $arr;
    }
}
